<?php

namespace IDock\Insurance\TPIC\Request;

use IDock\Common\Tools\XMLTools;
use IDock\Http\Request;
use IDock\Http\RequestFormat;
use IDock\Http\RequestMethod;
use IDock\Http\Response;
use IDock\Insurance\TPIC\Response\AllTPICResponse;

class AllTPICRequest extends Request {

    protected $code;

    protected $method = RequestMethod::POST;

    protected $format = RequestFormat::RAW;

    public $route;

    public function loadConfig(array $config): Request
    {
        $this->gateway = $config['gateway'] ?? '';

        $this->logsDir = $config['logsDir'] ?? '';

        $this->timeout = $config['timeout'] ?? '';

        $this->proxy = $config['proxy'] ?? '';

        $this->proxyPW = $config['proxyPW'] ?? '';

        $this->proxyAccount = $config['proxyAccount'] ?? '';

        return  $this;
    }

    /**
     * @return mixed
     */
    public function getCode()
    {
        return $this->code;
    }

//    public function request(Request $request): Response
//    {
//        return  parent::soapRequest($request);
//    }
    public function getStaffConfig(&$chalCode, &$userCode){

    }

    public function requestLogs($uuid, $inLog = true)
    {
        if (!$this->logsFile) {
            return;
        }

        if ($this->urlTip) {
            $url = $this->gateway;
            $urlLogs = date('Y-m-d H:i:s') . " FUN_TIPS " . $uuid . " " . $this->urlTip . ' ' . $url;
        } else {
            $urlLogs = date('Y-m-d H:i:s') . " URL      " . $uuid . " " . $this->gateway;
        }


        if (!$inLog) {
            return;
        }

        file_put_contents($this->logsFile, $urlLogs . "\r\n", FILE_APPEND);

        //header 日记
        if ($this->headers) {
            $encLogs = date('Y-m-d H:i:s') . " HEADER   " . $uuid . " " . json_encode($this->getHeaders(), JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
            file_put_contents($this->logsFile, $encLogs . "\r\n", FILE_APPEND);
        }

        if ($this->preParameter != $this->bizParam) {
            $encLogs = date('Y-m-d H:i:s') . " ENCRYPT  " . $uuid . " " . json_encode($this->preParameter, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
            file_put_contents($this->logsFile, $encLogs . "\r\n", FILE_APPEND);
        }

        if ($this->bizParam ?? '') {
            $str = $this->bizParam;
            if(strpos($str,'<imageBase64>') !== false){
                $start = strpos($str,'<imageBase64>') + strlen('<imageBase64>');
                $end = strpos($str,'</imageBase64>');
                $content = substr($str,$start ,$end - $start);
                $str = str_replace($content,'...',$str);
            }
            $reqLogs = date('Y-m-d H:i:s') . " REQUEST  " . $uuid . " " . json_encode($str, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
            file_put_contents($this->logsFile, $reqLogs . "\r\n", FILE_APPEND);
        }
    }

    public function responseLogs($uuid, $data)
    {
        if (!$this->logsFile) {
            return;
        }
        $repLogs = date('Y-m-d H:i:s') . " RESPONSE " . $uuid . " " . $data;
        $data = (new AllTPICResponse())->objectToArray(simplexml_load_string($data));
        if(isset($data['body']['elecPolicyInfoList']['elecFile'])){
            $data['body']['elecPolicyInfoList']['elecFile'] = '...';
            $repLogs = date('Y-m-d H:i:s') . " RESPONSE " . $uuid . " " . XMLTools::ToXML($data);
        }
        $repLogs = str_replace(["\r\n","\n","\r","\t"],['','','','',''],$repLogs);
        file_put_contents($this->logsFile, $repLogs . "\r\n", FILE_APPEND);
    }

}
